package com.atguigu.spzx.user.controller;

import com.atguigu.spyx.model.common.Result;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.h5.UserInfo;
import com.atguigu.spyx.model.dto.h5.UserRegistDto;
import com.atguigu.spzx.common.interceptor.SpzxUserInterceptor;
import com.atguigu.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LT
 * @create 2023-11-19-22:35
 */
@Data
@RequestMapping("/api/user/userInfo")
@RestController
public class UserInfoController {
    @Resource
    UserInfoService userInfoService;

    @Operation(summary = "注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegistDto userRegistDto){
        userInfoService.register(userRegistDto);
        return Result.ok();
    }

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody UserInfo userInfo){
        String token = userInfoService.login(userInfo);
        return Result.ok().data(token);
    }

    @Operation(summary = "获取登录信息")
    @GetMapping ("/auth/getCurrentUserInfo")
    public Result getCurrentUserInfo(){
        UserInfo userInfo = SpzxUserInterceptor.THREAD_LOCAL.get();
//        System.out.println(userInfo);
        Map<String,Object> map = new HashMap<>();
        map.put("nickName",userInfo.getNickName());
        map.put("avatar",userInfo.getAvatar());
        return Result.ok().data(map);
    }

    @Operation(summary = "注销接口")
    @GetMapping ("/logout")
    public Result logout(@RequestHeader("token")String token){
            userInfoService.logout(token);
            return Result.ok();
        }
    }

